package com.example.ranking;

import com.example.api.config.DefaultFeignConfig;
import com.example.ranking.service.RankingService;
import com.example.ranking.util.RankingGlobalConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableFeignClients(basePackages = "com.example.api.client", defaultConfiguration = DefaultFeignConfig.class)
@MapperScan("com.example.ranking.mapper")
@SpringBootApplication

@EnableScheduling
@RequiredArgsConstructor
@Slf4j
public class RankingApplication implements ApplicationRunner { // 改为实现ApplicationRunner
    public static void main(String[] args) {
        SpringApplication.run(RankingApplication.class, args);
    }

    private final RankingService rankingService;

    /**
     * 服务启动后执行：1. 全量统计初始化；2. 标记初始化完成（开启MQ消费）
     */
    // ApplicationRunner接口需实现ApplicationRunner的run方法（springboot2参数为ApplicationArguments）
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("Ranking微服务启动，开始全量统计初始数据");
        boolean initSuccess = rankingService.initPopularRanking();
        if (initSuccess) {
            RankingGlobalConfig.setMqConsumeEnabled(true);
            log.info("全量统计初始化成功，开启MQ消费");
        } else {
            throw new RuntimeException("全量统计初始化失败，Ranking微服务启动终止");
        }
    }
}
